package com.zyao89.view.zloading.text;

import com.zyao89.view.zloading.ZLoadingBuilder;
import com.zyao89.view.zloading.util.FloatRange;
import com.zyao89.view.zloading.util.NumCalcUtil;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.render.ColorFilter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextTool;
import ohos.app.Context;

/**
 * Created by zyao89 on 2017/4/9.
 * Contact me at 305161066@qq.com or zyao89@gmail.com
 * For more projects: https://github.com/zyao89
 * My Blog: http://zyao89.me
 */
public class TextBuilder extends ZLoadingBuilder {
    private static final float BASE_ALPHA = 0.4f;
    private static final String DEFAULT_TEXT = "Zyao89";
    private Paint mTextPaint;
    private String mTextChars;
    private int mDrawTextCount = 0;
    private Color color;

    @Override
    protected void initParams(Context context, Color color) {
        this.color = color;
        mTextPaint = new Paint();
        mTextPaint.setColor(color);
        mTextPaint.setDither(true);
        mTextPaint.setFilterBitmap(true);
        mTextPaint.setTextSize((int) getAllSize());
        mTextPaint.setStyle(Paint.Style.FILL_STYLE);
        mTextChars = DEFAULT_TEXT;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isNotEmpty()) {
            float measureText = mTextPaint.measureText(mTextChars);
            Paint paint = new Paint(mTextPaint);
            paint.setColor(color);
            paint.setAlpha(BASE_ALPHA);
            canvas.drawText(mTextPaint, mTextChars.substring(0, mDrawTextCount), NumCalcUtil.subtract(getViewCenterX(), measureText / 2), getViewCenterY());
            canvas.drawText(paint, mTextChars, NumCalcUtil.subtract(getViewCenterX(), measureText / 2), getViewCenterY());
        }
    }

    @Override
    protected void setAlpha(int alpha) {
        mTextPaint.setAlpha(alpha);
    }

    @Override
    protected void prepareStart(AnimatorValue floatValueAnimator) {
        System.out.println("prepareStart");
        floatValueAnimator.setDuration(ceil(getAnimationDuration() * 0.3f));
        floatValueAnimator.setCurveType(Animator.CurveType.ACCELERATE);
    }

    @Override
    protected void prepareEnd() {

    }

    @Override
    protected void computeUpdateValue(AnimatorValue animation, float animatedValue) {
       mTextPaint.setAlpha( NumCalcUtil.add(animatedValue * 0.6f, BASE_ALPHA));
    }

    @Override
    public void onRepeat(Animator animator) {
        if (isNotEmpty()) {
            if (++mDrawTextCount > mTextChars.toCharArray().length) {
                mDrawTextCount = 0;
            }
        }
    }

    @Override
    protected void setColorFilter(ColorFilter colorFilter) {
        mTextPaint.setColorFilter(colorFilter);
    }

    public void setText(String text) {
        if (TextTool.isNullOrEmpty(text)) {
            return;
        }
        float measureText = mTextPaint.measureText(text);
        if (measureText >= getIntrinsicWidth()) {
            float v = measureText / getAllSize();
            mTextPaint.setTextSize((int) (getIntrinsicWidth() / v));
        }
        mTextChars = text;
    }

    private boolean isNotEmpty() {
        return mTextChars != null && !mTextChars.isEmpty();
    }
}
